package gui.windows;

import gui.GUIHandler;
import gui.GUIManager;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.TitledBorder;

import tournament.Tournament;
import tournament.TournamentLayoutPanel;
import tournament.TournamentSystem;
import app.AppGlobals;
import javax.swing.JInternalFrame;

public class TournamentTab extends JPanel {//JTabbedPane {

	public TournamentTab(){
		this.setLayout(null);
		this.setBounds(0, 0, 976, 471);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(0, 0, 976, 471);
		this.add(tabbedPane);
		
		JPanel panel_23 = new JPanel();
		tabbedPane.addTab("Open Tournaments", null, panel_23, null);
		panel_23.setLayout(null);
		
		JPanel panel_24 = new JPanel();
		panel_24.setBorder(new TitledBorder(null, "Open Tournaments", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_24.setBounds(10, 11, 426, 362);
		panel_23.add(panel_24);
		panel_24.setLayout(null);
		
		JScrollPane scrollPane_5 = new JScrollPane();
		scrollPane_5.setBounds(10, 21, 406, 330);
		panel_24.add(scrollPane_5);
		
		
		DefaultListModel<Tournament> tournListModel = new DefaultListModel<Tournament>();
		final Tournament test = new Tournament();
		test.id = 1;
		test.name = "abc";
		test.currentBracket = 1;
		test.maxCompetitors = 16;
		AppGlobals.tournamentList.add(test);
		tournListModel.copyInto(AppGlobals.tournamentList.toArray());
		JList<Tournament> listboxOpenTournaments = new JList<Tournament>(tournListModel);
	

		//listboxOpenTournaments.setListData(AppGlobals.tournamentList.toArray());
		
		
		listboxOpenTournaments.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		scrollPane_5.setViewportView(listboxOpenTournaments);
		GUIManager.listboxOpenTournament = listboxOpenTournaments;
			JButton btnNewButton_1 = new JButton("Join");
		btnNewButton_1.setBounds(347, 381, 89, 23);
		panel_23.add(btnNewButton_1);
		
		JPanel panel_25 = new JPanel();
		panel_25.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Information", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_25.setBounds(451, 11, 223, 362);
		panel_23.add(panel_25);
		
		JPanel panel_26 = new JPanel();
		tabbedPane.addTab("Current Tournament", null, panel_26, null);
		panel_26.setLayout(null);
		
		JPanel panel_27 = new JPanel();
		panel_27.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Current Matches", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_27.setBounds(686, 11, 270, 183);
		panel_26.add(panel_27);
		panel_27.setLayout(null);
		
		JScrollPane scrollPane_6 = new JScrollPane();
		scrollPane_6.setBounds(10, 24, 250, 148);
		panel_27.add(scrollPane_6);
		
		JList listboxTournCompetitors = new JList();
		scrollPane_6.setViewportView(listboxTournCompetitors);
		
		JPanel panel_28 = new JPanel();
		panel_28.setBorder(new TitledBorder(null, "Status", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel_28.setBounds(686, 203, 270, 167);
		panel_26.add(panel_28);
		panel_28.setLayout(null);
		
		JLabel lblNextDuelIn = new JLabel("Next Duels In:");
		lblNextDuelIn.setBounds(10, 25, 86, 14);
		panel_28.add(lblNextDuelIn);
		
		JLabel lblNextDuelTimer = new JLabel("0 sec");
		lblNextDuelTimer.setBounds(130, 25, 33, 14);
		panel_28.add(lblNextDuelTimer);
		
		JLabel lblNewLabel = new JLabel("Current Opponent:");
		lblNewLabel.setBounds(10, 50, 110, 14);
		panel_28.add(lblNewLabel);
		
		JLabel lblCurOpponent = new JLabel("Opponent");
		lblCurOpponent.setBounds(130, 50, 86, 14);
		panel_28.add(lblCurOpponent);
		
		JLabel lblOppentStatus = new JLabel("Opponent Status:");
		lblOppentStatus.setBounds(10, 75, 110, 14);
		panel_28.add(lblOppentStatus);
		
		JLabel lblNotReady = new JLabel("Not Ready");
		lblNotReady.setBounds(130, 75, 110, 14);
		panel_28.add(lblNotReady);
		
		JCheckBox chckbxAreYouReady = new JCheckBox("Are you Ready?");
		chckbxAreYouReady.setBounds(10, 96, 122, 23);
		panel_28.add(chckbxAreYouReady);
		
		JButton btnJoinMatch = new JButton("Join Match");
		btnJoinMatch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Tournament tourn = new Tournament();
				tourn.initCompetitorBoard();
				tourn.setPositionLabel(1, 2, "Terry");
				TournamentSystem.updateTournamentListbox();
				
			}
		});
		btnJoinMatch.setBounds(171, 122, 89, 23);
		panel_28.add(btnJoinMatch);
		
		JLabel lblTimeLeft = new JLabel("Time left to join match:  ");
		lblTimeLeft.setBounds(10, 126, 122, 14);
		panel_28.add(lblTimeLeft);
		
		JLabel lblSec = new JLabel("0 sec");
		lblSec.setBounds(130, 126, 68, 14);
		panel_28.add(lblSec);
		
		JButton btnLeaveTournament = new JButton("Leave Tournament");
		btnLeaveTournament.setBounds(813, 381, 143, 23);
		panel_26.add(btnLeaveTournament);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 11, 666, 359);
		panel_26.add(scrollPane);
		
		TournamentLayoutPanel panel = new TournamentLayoutPanel(test);
		panel.graphComponent.getGraphControl().setBorder(null);
		scrollPane.setViewportView(panel);
		
		JButton btnNewButton = new JButton("Layout");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				GUIHandler.launchTournamentLayoutDialog(test);
			}
		});
		btnNewButton.setBounds(10, 381, 89, 23);
		panel_26.add(btnNewButton);
		
	}
}
